package com.example.appointment.widget;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.appointment.R;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Admin on 2016/10/14.
 */

public class BottomSwitchPopWindows extends PopupWindow {

    @Bind(R.id.cancel_btn)
    TextView cancelBtn;
    @Bind(R.id.enter_btn)
    TextView enterBtn;
    @Bind(R.id.switch_wheel)
    WheelView switchWheel;
    @Bind(R.id.pop_parent_view)
    RelativeLayout popParentView;
    private Context mContext;

    private ArrayList<String> saxList = new ArrayList<>();
    private ArrayList<String> dataList = new ArrayList<>();

    public final int SAX_TYPE = 0;
    public final int DATA_TYPE = 1;

    private String switchItems;

    public interface OnCancelClick {
        void onCancelClick();
    }

    public interface OnEnterClick {
        void onEnterClick();
    }

    private OnCancelClick onCancelClick;
    private OnEnterClick onEnterClick;

    public void setOnEnterClick(OnEnterClick onEnterClick) {
        this.onEnterClick = onEnterClick;
    }

    public void setOnCancelClick(OnCancelClick onCancelClick) {
        this.onCancelClick = onCancelClick;
    }

    public BottomSwitchPopWindows(Context context) {
        this.mContext = context;
        View view = LayoutInflater.from(mContext).inflate(R.layout.bottom_switch_popwindows, null);

        setContentView(view);
        ButterKnife.bind(this, view);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        // 设置动画弹出效果
        // mPopupWindow.setAnimationStyle(R.style.PopupAnimation);
        // 设置半透明黑色
        ColorDrawable dw = new ColorDrawable(Color.parseColor("#00000000"));
        setBackgroundDrawable(dw);

        setClippingEnabled(true);
        setFocusable(true);
        setOutsideTouchable(true);
    }

    @OnClick({R.id.cancel_btn, R.id.enter_btn,R.id.pop_parent_view})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cancel_btn:
                //取消
                onCancelClick.onCancelClick();
                break;
            case R.id.enter_btn:
                //获取当前选择的item
                switchItems = switchWheel.getSeletedItem();
                //确定
                onEnterClick.onEnterClick();
                break;
            case R.id.pop_parent_view:
                dismiss();
                break;
        }
    }

    /**
     * 添加性别选项
     */
    private void addSaxList() {
        saxList.add("男");
        saxList.add("女");
        saxList.add("保密");

        switchWheel.setItems(saxList);
    }

    /**
     * 直接滚到之前选择过的item
     */
    private int getScrollItem(int type, String str) {
        switch (type) {
            case SAX_TYPE:
                if (saxList.indexOf(str) >= 0) {
                    return saxList.indexOf(str);
                }
                break;
            case DATA_TYPE:
                if (dataList.indexOf(str) >= 0) {
                    return dataList.indexOf(str);
                }
                break;
        }

        return 0;
    }

    /**
     * 添加年龄选项
     */
    private void addDataList() {
        dataList.add("1960");
        dataList.add("1970");
        dataList.add("1980");
        dataList.add("1990");
        dataList.add("2000");

        switchWheel.setItems(dataList);
    }

    /**
     * 选择加载哪个选择器
     *
     * @param type 0 性别 1 年龄
     */
    public void SwitchWheelView(int type, String str) {
        switch (type) {
            case SAX_TYPE:
                addSaxList();
                switchWheel.setSeletion(getScrollItem(type, str));
                break;
            case DATA_TYPE:
                addDataList();
                switchWheel.setSeletion(getScrollItem(type, str));
                break;
        }
    }

    public String getSwitchItems() {
        return switchItems;
    }

    public void setSwitchItems(String switchItems) {
        this.switchItems = switchItems;
    }

    public void showAtCenter(View view) {
        showAtLocation(view, Gravity.BOTTOM, 0, 0);
    }
}
